define(function(require, exports, module) {
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var ajax = require("resource/js/component/ajax.js");
	require("oa/act/approval/progress/css/progress.css");
	var initPage = function() {
		ajax.init().success(function(code, msg, data) {
			if (data != undefined || data != null) {
				var html="";
				$("#setUser").html("");
				$.each(data, function(i, val) {
					var userinfo = {
						userid: val.userid,
						usertype: val.usertype
					};
					indexApp.setUserIds.push(userinfo);
					if (i != 0) {
						html += '<li><span class="OAimgArrow"><img src="' + BASEPATH + '/act/images/arrow-right-oa.png" /></span></li>';
					}
					var url = "/resource/images/user_icon.png";
					if (val.imgurl != undefined && val.imgurl != "") {
						url = val.imgurl;
					}
					html += '<li><span class="OAimgICON"><img src="' + BASEPATH + url + '" /></span><span>' + val.username + '</span></li>';
				});
				$("#setUser").html(html);
			}
		}).error(function(code, msg, data) {
			$.messager.alert("系统提示", msg);
		}).post(BASEPATH + "/oa/act/actExUserDef/getUserImgs.htm", {
			processKey: $("#processKey").val(),
			isDefined: $("#isDefined").val()
		});
		btnClickBind();
		initTree();
	};

	function btnClickBind() {
		$("#userBtn").bind("click", function() {
			$("#userDiv").show();
			$("#deptDiv").hide();
			$("#roleDiv").hide();
			$("#userBtn").attr("class", "button select");
			$("#deptBtn").attr("class", "button");
			$("#roleBtn").attr("class", "button");
		});
		$("#deptBtn").bind("click", function() {
			$("#userDiv").hide();
			$("#deptDiv").show();
			$("#roleDiv").hide();
			$("#userBtn").attr("class", "button");
			$("#deptBtn").attr("class", "button select");
			$("#roleBtn").attr("class", "button");

		});

		$("#roleBtn").bind("click", function() {
			$("#userDiv").hide();
			$("#deptDiv").hide();
			$("#roleDiv").show();
			$("#userBtn").attr("class", "button");
			$("#deptBtn").attr("class", "button");
			$("#roleBtn").attr("class", "button select");

		});
		$("#directorBtn").bind("click", function() {
			var setUser = $("#setUser").html();
			var nodeNum = $("#nodeNum").val();
			if (indexApp.setUserIds != null && indexApp.setUserIds.length > 0) {
				if (indexApp.setUserIds.length >= nodeNum) {
					$.messager.alert("系统提示", "无法添加处理人，该流程只能设置" + nodeNum + "步处理人！");
					return;
				}
				setUser = setUser + '<li><span class="OAimgArrow"><img src="' + BASEPATH + '/act/images/arrow-right-oa.png" /></span></li>';
			}
			var userinfo = {
				userid: 'director',
				usertype: "director"
			};
			indexApp.setUserIds.push(userinfo);
			var url = "/resource/images/user_icon.png";
			$("#setUser").html(setUser + '<li><span class="OAimgICON"><img src="' + BASEPATH + url + '" /></span><span>主管人员</span></li>');
		});

	}
	var initTree = function() {
		require("resource/js/component/tree.js");
		using("ztree", function() {
			var setting = {
				data: {
					simpleData: {
						enable: true,
						idKey: 'id',
						pIdKey: 'pid'
					}
				},
				callback: {
					onClick: onClick
				}
			};
			ajax.init().success(function(code, msg, zTreeNodes) {
				for (var i = 0; i < zTreeNodes.length; i++) {
					if (zTreeNodes[i].type == "DEPT") {
						zTreeNodes[i].open = true;
						zTreeNodes[i].iconSkin = "diy01";
					} else {
						zTreeNodes[i].iconSkin = "diy02";
					}
				}
				$.fn.zTree.init($("#userTree"), setting, zTreeNodes);
			}).error(function(code, msg, data) {
				$.messager.alert("系统提示", msg);
			}).post(BASEPATH + "/base/authoriza/user/getUserListByDeptId.htm");

			var deptsetting = {
				data: {
					simpleData: {
						enable: true,
						idKey: 'id',
						pIdKey: 'pId'
					}
				},
				callback: {
					onClick: onClickDept
				}
			};
			ajax.init().success(function(code, msg, zTreeNodes) {
				for (var i = 0; i < zTreeNodes.length; i++) {
					zTreeNodes[i].open = true;
					if (zTreeNodes[i].pId) {
						zTreeNodes[i].iconSkin = "diy03";
					} else {
						zTreeNodes[i].iconSkin = "diy01";
					}
				}
				$.fn.zTree.init($("#deptTree"), deptsetting, zTreeNodes);
			}).error(function(code, msg, data) {
				$.messager.alert("系统提示", msg);
			}).post(BASEPATH + "/base/authoriza/dept/queryDeptTreeByRootDept.htm");

			var rolesetting = {
				data: {
					simpleData: {
						enable: true,
						idKey: 'roleId',
						pIdKey: 'pId'
					}
				},
				callback: {
					onClick: onClickRole
				}
			};
			ajax.init().success(function(code, msg, zTreeNodes) {
				for (var i = 0; i < zTreeNodes.length; i++) {
					zTreeNodes[i].open = true;
//					if (zTreeNodes[i].pId) {
//						zTreeNodes[i].iconSkin = "diy03";
//					} else {
//						zTreeNodes[i].iconSkin = "diy01";
//					}
					zTreeNodes[i].id = zTreeNodes[i].roleId;
					zTreeNodes[i].name = zTreeNodes[i].roleName;
				}
				$.fn.zTree.init($("#roleTree"), rolesetting, zTreeNodes);
			}).error(function(code, msg, data) {
				$.messager.alert("系统提示", msg);
			}).post(BASEPATH + "/oa/oarole/queryOARole.htm");
		});
	};

	function onClick(event, treeId, treeNode) {
		if (treeNode.type == 'USER') {
			var setUser = $("#setUser").html();
			var nodeNum = $("#nodeNum").val();
			if (indexApp.setUserIds != null && indexApp.setUserIds.length > 0) {
				if (indexApp.setUserIds.length >= nodeNum) {
					$.messager.alert("系统提示", "无法添加处理人，该流程只能设置" + nodeNum + "步处理人！");
					return;
				}
				setUser = setUser + '<li><span class="OAimgArrow"><img src="' + BASEPATH + '/act/images/arrow-right-oa.png" /></span></li>';
			}
			var userinfo = {
				userid: treeNode.id,
				usertype: "userid"
			};
			indexApp.setUserIds.push(userinfo);
			var url = "/resource/images/user_icon.png";
			if (treeNode.imgurl != undefined && treeNode.imgurl != "") {
				url = treeNode.imgurl;
			}
			$("#setUser").html(setUser + '<li><span class="OAimgICON"><img src="' + BASEPATH + url + '" /></span><span>' + treeNode.name + '</span></li>');
		}
	}

	function onClickDept(event, treeId, treeNode) {
		var setUser = $("#setUser").html();
		var nodeNum = $("#nodeNum").val();
		if (indexApp.setUserIds != null && indexApp.setUserIds.length > 0) {
			if (indexApp.setUserIds.length >= nodeNum) {
				$.messager.alert("系统提示", "无法添加处理人，该流程只能设置" + nodeNum + "步处理人！");
				return;
			}
			setUser = setUser + '<li><span class="OAimgArrow"><img src="' + BASEPATH + '/act/images/arrow-right-oa.png" /></span></li>';
		}
		var userinfo = {
			userid: treeNode.id,
			usertype: "deptid"
		};
		indexApp.setUserIds.push(userinfo);
		var url = "/resource/images/sy_default.png";
		$("#setUser").html(setUser + '<li><span class="OAimgICON"><img src="' + BASEPATH + url + '" /></span><span>' + treeNode.name + '</span></li>');
	}

	function onClickRole(event, treeId, treeNode) {
		var setUser = $("#setUser").html();
		var nodeNum = $("#nodeNum").val();
		if (indexApp.setUserIds != null && indexApp.setUserIds.length > 0) {
			if (indexApp.setUserIds.length >= nodeNum) {
				$.messager.alert("系统提示", "无法添加处理人，该流程只能设置" + nodeNum + "步处理人！");
				return;
			}
			setUser = setUser + '<li><span class="OAimgArrow"><img src="' + BASEPATH + '/act/images/arrow-right-oa.png" /></span></li>';
		}
		var userinfo = {
			userid: treeNode.id,
			usertype: "roleid"
		};
		indexApp.setUserIds.push(userinfo);
		var url = "/resource/images/sy_default.png";
		$("#setUser").html(setUser + '<li><span class="OAimgICON"><img src="' + BASEPATH + url + '" /></span><span>' + treeNode.name + '</span></li>');
	}


	module.exports = {
		init: function() {
			using(["datagrid", "droppable", "draggable"], function() {
				initPage();
			});
		}
	}
});